package com.hackvan2012.datastore.factory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import com.hackvan2012.api.YellowAPIService;
import com.hackvan2012.datastore.dao.*;

public class DataFactory {
	
	/** Unique id for each food entry. */
	private static long entryID = 255;

	/**
	 * Creates a brand new FoodEntryObject.
	 * @param imgURL		The URL of the image file.
	 * @param dishName		The name of the food dish.
	 * @param restaurant	The restaurant where the food comes from.
	 * @param title			The title of the image given by the user.
	 * @param description	Any user comments on the food entry.
	 * @return A new FoodEntryObject with a unique ID and the current date and time.
	 */
	public static FoodEntryObject createFoodEntry(String imgURL, String dishName, String restaurant, String title, String description) {
		
		YellowAPIService ys = new YellowAPIService(restaurant.replaceAll(" ", "+").toLowerCase(), "vancouver", true);
		String result = ys.call();
		result = result.replaceAll("\n", "");
		String pid = result.substring(0, 499);
		int begin = result.indexOf("parentId=\"");
		int end = result.indexOf("\" isParent");
		result = result.substring(begin+("parentId=\"".length()), end);
		pid = restaurant +" pid: "+result;
		
		return new FoodEntryObject(String.valueOf(++entryID), imgURL, dishName, title, description, new Date(), pid, 0);
	}
	
	/**
	 * Generates a brand new PostObject
	 * @param parent	The FoodEntryObject that this post refers to.
	 * @param text		The text that this post contains.
	 * @return A new PostObject with the current date and time.
	 */
	public static PostObject createPost(String parent, Text text) {
		return new PostObject(parent, new Date(), text);
	}
	
	/**
	 * Generates a new Tag object.
	 * @param name The name of the tag.
	 * @return The new TagObject with the given name. It starts with a count of 0.
	 */
	public static TagObject createTag(String name) {
		return new TagObject(name, 0);
	}
	
	/**
	 * Generates a new Tag Pointer object
	 * @param pointer The tag that this TagPointer refers to.
	 * @return The new TagPointerObject.
	 */
	public static TagPointerObject createTagPointer(Key pointer) {
		return new TagPointerObject(pointer);
	}
}
